import { defineStore } from 'pinia';
import type { UploadedImage } from '../types/models';
import imageUploadService from '../services/ImageUploadService';

export const useUploadStore = defineStore('upload', {
  state: () => ({
    images: [] as UploadedImage[],
    isUploading: false,
    uploadProgress: 0
  }),
  
  actions: {
    async uploadImages(files: File[]) {
      this.isUploading = true;
      this.uploadProgress = 0;
      
      try {
        const uploadedImages = await imageUploadService.uploadImages(files, (progress) => {
          this.uploadProgress = progress;
        });
        
        this.images = [...this.images, ...uploadedImages];
        return uploadedImages;
      } finally {
        this.isUploading = false;
        this.uploadProgress = 100;
      }
    },
    
    removeImage(id: string) {
      this.images = this.images.filter(image => image.id !== id);
    },
    
    validateImage(file: File) {
      return imageUploadService.validateImage(file);
    },
    
    // 添加新方法
    setUploading(value: boolean) {
      this.isUploading = value;
    },
    
    setUploadProgress(progress: number) {
      this.uploadProgress = progress;
    },
    
    addImages(newImages: UploadedImage[]) {
      this.images = [...this.images, ...newImages];
    }
  }
});